In [1]:
import cv2
from cv2 import VideoCapture
from sklearn.svm import SVC
from os import path
import os
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
In [2]:
n_pos = len(os.listdir('target/'))
n_neg = len(os.listdir('error/'))
dataset = [('target/' + f, 1) for f in os.listdir('target/')] + [('error/' + f, 0) for f in os.listdir('error/')]
for i in np.random.choice(os.listdir('target/'), size=(n_neg - n_pos), replace=True):
    dataset.append(('target/' + i, 1))
dataset = np.array(dataset)
N = len(dataset)
val_id = np.random.choice(range(N), N // 10, replace=False)
val_set = dataset[val_id]
train_set = dataset[~val_id]
In [3]:
img = cv2.imread('target/00020240.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
Out[3]:
<matplotlib.image.AxesImage at 0x7fef6323e310>
In [4]:
def feature(image_name):
    feat = []
    if isinstance(image_name, basestring):
        img = cv2.imread(image_name)
    else:
        img = image_name
    feat.append(img.mean(axis=(0, 1)))
    for x in np.hsplit(img, 3):    # x is (1080, 640, 3)
        for y in np.vsplit(x, 3):  # y is (360, 640, 3)
            feat.append(y.mean(axis=(0, 1)))
    
    return np.array(feat).flatten()
In [5]:
def featurize(dataset):
    X = []
    y = []
    for img, tag in dataset:
        try:
            X.append(feature(img))
            y.append(int(tag))
        except:
            print img, 'is not an image'
            pass
    X = np.array(X)
    y = np.array(y)
    return X, y
In [6]:
X, y = featurize(train_set)
VX, Vy = featurize(val_set)
np.mean(y), np.mean(Vy)
target/test.py is not an image
target/.DS_Store is not an image
target/test.py is not an image
target/.DS_Store is not an image
target/.DS_Store is not an image
target/.DS_Store is not an image
target/test.py is not an image
Out[6]:
(0.5174825174825175, 0.47120418848167539)
In [19]:
classifier = SVC(probability=True)
classifier.fit(X, y)
Out[19]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=True, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
In [20]:
predy = classifier.predict(VX)
print 'total accuracy', np.mean(predy == Vy)
print 'false positive', np.mean((Vy == 0) & (predy == 1))
print 'false negative', np.mean((Vy == 1) & (predy == 0))
total accuracy 0.970331588133
false positive 0.0
false negative 0.0296684118674
In [44]:
def video_feature(name):
    vid = VideoCapture(name)
    frames = []
    feat = []
    i = 0
    while True:
        i += 1
        if i < 10:
            continue
        i = 0
        ret, img = vid.read()
        if not ret:
            break
        feat.append(feature(img))
        frames.append(img)
    return np.array(feat), np.array(frames)
In [45]:
TX, frame = video_feature('001e4073d0aaef120fb35e9175370297.mp4')
In [46]:
TX.shape
Out[46]:
(3532, 30)
In [47]:
Ty = classifier.predict(TX)
In [48]:
Ty.sum()
Out[48]:
0
In [59]:
z = []
for i in range(len(TX) - 1):
    z.append(np.linalg.norm(TX[i] - TX[i + 1]))
    if z[-1] > 50:
        print i, z[-1]
        plt.imshow(cv2.cvtColor(frame[i], cv2.COLOR_BGR2RGB))
        plt.show()
plt.plot(z)
119 320.848323015
209 52.1775430171
292 332.885010695
418 339.173193511
708 247.398446461
760 181.723112969
1188 215.566330869
1298 201.615005327
1383 60.1065765339
1401 55.4218259593
1402 84.8028753433
1403 88.5961462539
1404 127.501885673
1405 197.042132545
1406 186.103144418
1573 60.0217518863
1574 68.8841408726
1575 81.4956192019
1576 136.938631539
1577 180.134794892
1578 165.889817259
1941 193.000600806
2044 197.828110632
2257 255.759956635
2375 321.87023667
2517 84.5496178128
2518 100.82588597
2519 105.839270191
2520 171.071592335
2521 173.229773911
3005 97.4353212374
3006 108.032735363
3007 101.811156201
3008 175.919389062
3009 187.175204294
3217 157.06331986
3359 177.137442249
3518 279.13576883
Out[59]:
[<matplotlib.lines.Line2D at 0x7fef49cbdc90>]
In [82]:
def find_continue_group(ary):
    group_start = -1
    for a, b in zip(ary[:-1], ary[1:]):
        if b - a > 1:
            if group_start > 0 and group_len > 1:
                print group_start, group_len
            group_start = b
            group_len = 0
        elif b - a == 1 and group_start > 0:
            group_len += 1
In [79]:
np.where(np.array(z) > 50)[0]
Out[79]:
array([ 119,  209,  292,  418,  708,  760, 1188, 1298, 1383, 1401, 1402,
       1403, 1404, 1405, 1406, 1573, 1574, 1575, 1576, 1577, 1578, 1941,
       2044, 2257, 2375, 2517, 2518, 2519, 2520, 2521, 3005, 3006, 3007,
       3008, 3009, 3217, 3359, 3518])
In [83]:
find_continue_group(np.where(np.array(z) > 50)[0])
1401 5
1573 5
2517 4
3005 4
In [88]:
for i in range(2517 + 20, 3005 - 20, 100):
    plt.imshow(cv2.cvtColor(frame[i], cv2.COLOR_BGR2RGB))
    plt.show()
In [86]:
for i in range(1401 + 20, 1573 - 20, 10):
    plt.imshow(cv2.cvtColor(frame[i], cv2.COLOR_BGR2RGB))
    plt.show()
In [ ]: